package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BuildingModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 增加店铺员工
 */
public class UseAddStaff extends BaseUseGoods {

	public UseAddStaff(GamePlayer player, GoodsInfo goodsInfo, long param) {
		super(player, goodsInfo, param);
	}

	@Override
	int checkUse(long count) {
		UserBuilding userBuilding = player.getModule(BuildingModule.class).getRandomBuilding();
		if(null == userBuilding){
			return GameErrorCode.E_BAG_ITEM_USE_NO_BUILDING;
		}
		return 0;
	}

	@Override
	UseGoodsResult executeUse(long count) {
		UseGoodsResult result = new UseGoodsResult();
		int addStaff = goodsInfo.getParamList().get(0).intValue();
		Property property = new Property();
		List<UserBuilding> syncList = new ArrayList<>();
		BuildingModule buildingModule = player.getModule(BuildingModule.class);
		int totalAddCount = 0;

		// 标记建筑被加了多少次
		Map<Integer, Integer> buildingAddNumMap = new HashMap<>();

		for(int i = 0; i < count; i++) {
			UserBuilding userBuilding = buildingModule.getRandomBuilding();
			if(null != userBuilding){

				totalAddCount = totalAddCount + addStaff;
				property.addProperty(userBuilding.getBuildingId(), BigInteger.valueOf(addStaff));
				if(!syncList.contains(userBuilding)){
					syncList.add(userBuilding);
				}

				if (!buildingAddNumMap.containsKey(userBuilding.getBuildingId())) {
					buildingAddNumMap.put(userBuilding.getBuildingId(), 0);
				}
				buildingAddNumMap.put(userBuilding.getBuildingId(), buildingAddNumMap.get(userBuilding.getBuildingId()) + 1);

			}
		}

		// 统一日志及逻辑处理 防止因道具一次使用太多导致逻辑冗余处理及日志冗余
		for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
			int addBuildingId = entry.getKey();
			int addValue = entry.getValue().intValue();
			UserBuilding addUserBuilding = buildingModule.getBuildingById(addBuildingId);

			if (addUserBuilding != null) {
				int oldValue = addUserBuilding.getStaffCount();
				//增加员工数量，游历不考虑店铺等级限制员工数量
				addUserBuilding.setStaffCount(addUserBuilding.getStaffCount() + addValue);
				LogMgr.addLogUseAddAttributeGoods(player.getUserId(), goodsInfo.getGoodsId(), goodsInfo.getType(), addBuildingId, addValue,
						BigInteger.valueOf(oldValue), BigInteger.valueOf(addUserBuilding.getStaffCount()), buildingAddNumMap.getOrDefault(addBuildingId, 0));
			}

		}


		if(syncList.size() > 0){
			buildingModule.notifyStaffTask(totalAddCount);
			buildingModule.onBuildingChange();
			buildingModule.syncBuilding(syncList, false);

			//for (UserBuilding building : syncList) {
				//商铺招募伙计
				//DataAnalyticsMgr.trackBuildingRecruit(player, building.getBuildingId(), building, property.getCountByGoodsId(building.getBuildingId()).intValue(), 1, building.getBuildingEarnSpeed());
			//}
		}
		result.setReward(PropertyHelper.parsePropertyToString(property));
		return result;
	}
}
